package com.mystudy.selfCheck;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.crypto.dsig.keyinfo.KeyName;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: infoalgorithm
 * @description: 测试函数式接口
 * @author: zhouzhilong
 * @create: 2019-05-24 10:53
 **/
public class FunctionInterfaceDemo {
    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(FunctionInterfaceDemo.class);

    @FunctionalInterface
    interface Predicate<T> {
        boolean test(T t);
    }

    public static boolean doPredicate(int age, Predicate<Integer> predicate) {
        return predicate.test(age);
    }

    public static void main(String[] args) {
        boolean isAdult = doPredicate(88, (x) -> {
            return x >= 5;
        });
        System.out.println(isAdult);
    }


    @Test
    public void testConsumer() {
        donation(1000, (money) -> System.out.println("我花了" + money + "元"));
    }

    private void donation(Integer money, Consumer<Integer> consumer) {
        consumer.accept(money);
    }

    @Test
    public void testSupply() {
        List<Integer> supply = supply(10, () -> (int) (Math.random() * 100));
        supply.forEach(item -> System.out.println(item));
    }

    private List<Integer> supply(Integer num, Supplier<Integer> supplier) {
        List<Integer> result = new ArrayList<Integer>();
        for (int i = 0; i < num; i++) {
            result.add(supplier.get());
        }
        return result;

    }

    @Test
    public void testFunction() {
        Integer convert = convert("3124", x -> Integer.parseInt(x));
        System.out.println(convert);
    }

    private Integer convert(String str, Function<String, Integer> function) {
        return function.apply(str);
    }


    @Test
    public void testPredicate() {
        List<String> fruit = Arrays.asList("香蕉", "哈密瓜", "榴莲", "火龙果", "水蜜桃");
        List<String> newFruit = filter(fruit, (f) -> f.length() == 2);
        System.out.println(newFruit);
    }

    private List<String> filter(List<String> fruit, Predicate<String> predicate) {
        List<String> f = new ArrayList<>();
        for (String s : fruit) {
            if (predicate.test(s)) {
                f.add(s);
            }
        }
        return f;
    }


    @Test
    public void match() {
        List<String> cases = init();
        String pattern = "S\\d*K((p[a-z])?\\d{4,}(-)?)*B((p[a-z])?\\d{4,}(-)?)*.*";
        for (String aCase : cases) {
            boolean matches = aCase.matches(pattern);
            if (!matches) {
                LOGGER.warn("{}  matches = [{}]", aCase, matches);
            } else {
                Integer system = getSystem(aCase);
                if (system != null) {
//                    LOGGER.debug("system = [{}]",system);
                }
                Integer kernel = getKernel(aCase, "pc");
                if (kernel != null) {
//                    LOGGER.debug("kernel = [{}]", kernel);
                }

                //获取Kernel
                //获取bootloader

            }
        }

    }

    /**
     * 如果是数字-数字,那么返回最小的那个
     * 如果是pc数字-pb数字,找出自己类型的哪个
     * 如果是数字,返回数字
     *
     * @param source
     * @param type
     * @return
     */
    private Integer getKernel(String source, String type) {
        Integer result = null;
        //获取System
        Pattern systemPatter = Pattern.compile("K((p[a-z])?\\d{4,}(-)?)*");
        Matcher matcher = systemPatter.matcher(source);
        matcher.find();
        String kernel = matcher.group();
        LOGGER.debug("source = {},kernel = [{}]", source, kernel);

        //提取数字
        Pattern kernelDegitalPatter = Pattern.compile("(" + type + ")?\\d{4,}");
        Matcher matcher2 = kernelDegitalPatter.matcher(kernel);
        boolean haveKernelCode = matcher2.find();
        if (haveKernelCode) {
            String kernelCode = matcher2.group();
            if (kernelCode.indexOf(type) != -1) {
                result = getDegital(kernelCode);
                return result;
            }else{

            }
        }

        Matcher matcher1 = kernelDegitalPatter.matcher(kernel);
        boolean b = matcher1.find();
        if (b) {
            String kernelDegital = matcher1.group();
            LOGGER.debug("kernelDegital = [{}]", kernelDegital);

            boolean b1 = matcher1.find();
            if (b1) {
                String group = matcher1.group();
                LOGGER.debug("group = [{}]", group);
            }
//            if (StringUtils.isNotBlank(kernelDegital)) {
//                LOGGER.debug("kernel = [{}]", kernelDegital);
//                result = Integer.parseInt(kernelDegital);
//            }
        }

        return 0;
    }

    private Integer getSystem(String source) {
        Integer result = null;
        //获取System
        Pattern systemPatter = Pattern.compile("S\\d*");
        Matcher matcher = systemPatter.matcher(source);
        matcher.find();
        String system = matcher.group();

        result = getDegital(system);
        return result;
    }

    /**
     * 获取字符串中第一个串数字
     *
     * @param source
     * @return
     */
    private Integer getDegital(String source) {
        Integer result = null;
        Pattern degitalPatter = Pattern.compile("\\d+");
        Matcher matcher = degitalPatter.matcher(source);
        boolean finded = matcher.find();
        if (finded) {
            String degital = matcher.group();
            if (StringUtils.isNotBlank(degital)) {
                result = Integer.parseInt(degital);
            }
        }
        return result;
    }


    @Test
    public void tiqu() throws IOException {
        FileReader fileReader = new FileReader("/home/wizarpos/Downloads/test_FirmwareSingle.py");
        BufferedReader reader = new BufferedReader(fileReader);

        FileWriter writer = new FileWriter("/home/wizarpos/Nutstore Files/Nutstore/code_repo/infoalgorithm/src/test/java/com/mystudy/selfCheck/1.txt");
        writer.append("private static final List cases =new ArrayList<>();");
        String read = reader.readLine();
        while (read != null) {
            int i = read.indexOf("appid[1]=");
            if (i != -1) {
                String item = read.substring(i + 9).trim();
                LOGGER.debug("item = [{}]", item);
                writer.append("cases.add(\"" + item.replace("'", "") + "\");\n");
            }
            read = reader.readLine();
        }
        writer.append("]");
        writer.flush();

    }


    private List init() {
        List cases = new ArrayList<>();
        cases.add("S303668KBMWROP");
        cases.add("S303668KBMWROP");
        cases.add("S303668KBMWROP");
        cases.add("S3036KBMWROP");
        cases.add("S6603KBMWROP");
        cases.add("S1111KBMWROP");
        cases.add("S1111KBMWROP");
        cases.add("S1111KBMWROP");
        cases.add("S1111KBMWROP");
        cases.add("S1111KBMWROP");
        cases.add("SKpc2508BMWROP");
        cases.add("SKBpc3926MWROP");
        cases.add("SKBpc3372-pb3379MWROP");
        cases.add("SKBpc3372-pb3379MWROP");
        cases.add("SKBpa3372-pb3379-pd3379MWROP");
        cases.add("SKBpc303372MWROP");
        cases.add("SKBpc303372MWROP");
        cases.add("SKBpc303373MWROP");
        cases.add("SKBpc303372MWROP");
        cases.add("SKBpc303372MWROP");
        cases.add("SKBpc303373MWROP");
        cases.add("SKBpc3926MWROP");
        cases.add("SKBpc3926MWROP");
        cases.add("SKBpc3926MWROP");
        cases.add("SKBpc3926MWROP");
        cases.add("SKBMWpb56ROP");
        cases.add("SKBMWpb56ROP");
        cases.add("SKBMWpb11ROP");
        cases.add("SKBMWpb11ROP");
        cases.add("SKBMWpb52ROP");
        cases.add("SKBMWpb52ROP");
        cases.add("SKBMpc20191217WROP");
        cases.add("SKBMpc20191217-pa20191219WROP");
        cases.add("SKBMpc20191219-pa20191211WROP");
        cases.add("SKBMpc20191218-pa20191230WROP");
        cases.add("SKBMpd20191218-pa20191230WROP");
        cases.add("SKBMpc20191217WROP");
        cases.add("S3895K3885-3876Bpb3885-pc3876Mpb20190730-pc20191107Wpb52Rpc3473-pb3415O2551P2680");
        cases.add("S3895K3885Bpb3885-pc3876Mpb20190730-pc20191107Wpb52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20191107Wpb52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20191107Wpb52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20191107Wpb52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20191107Wpb52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20191107Wpb52Rpc3473-pb3415O2551P2680");
        cases.add("SKpb3885Bpc3876MWROP");
        cases.add("SKBMWROP");
        cases.add("S9999KBMWROP");
        cases.add("S1111KBMWROP");
        cases.add("S1111KBMWROP");
        cases.add("S1111KBMWROP");
        cases.add("S0001KBMWROP");
        cases.add("S9999KBMWROP");
        cases.add("S9999KBMWROP");
        cases.add("S9998KBMWROP");
        cases.add("S999998KBMWROP");
        cases.add("S999998KBMWROP");
        cases.add("SKBpc3926MWROP");
        cases.add("SKBpc3926MWROP");
        cases.add("SKBMWpb11ROP");
        cases.add("SKBMWpb10ROP");
        cases.add("SKBMWpc58ROP	");
        cases.add("SKBMWpb56-pc58ROP");
        cases.add("SKBMWpb56-pc58ROP");
        cases.add("SKBMWpb56-pc58ROP");
        cases.add("SKBMW58ROP");
        cases.add("SKBMW11ROP");
        cases.add("SKBMW09ROP");
        cases.add("SKBpc3876MW56ROP");
        cases.add("SKBpc0001MW11ROP");
        cases.add("SKBpc9990MW11ROP");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20191107W52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("SKBMpc20191219WROP");
        cases.add("SKBMpc20191219WROP");
        cases.add("SKBMpc20191218WROP");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20191108Wpb52Rpc3473-pb3415O2551P2680");
        cases.add("S3895Kpb3885-pc3876Bpb3885-pc3876Mpb20190730-pc20211107W52Rpc3473-pb3415O2551P2680");
        cases.add("SKBMW52ROP");
        cases.add("SKBMW53ROP");
        cases.add("SKBMW53ROP");
        cases.add("SKpc3996-pb3000-pa3999-pe3999BMWROP");
        cases.add("SKpc3996BMWROP");
        cases.add("SKpc993996BMWROP");
        cases.add("S3877Kpb3005-pc3876Bpb3885-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("S3877Kpc3876Bpb3885-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("S3877Kpb3999-pc3876Bpb3885-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("SKBpc3996-pb3000-pa3999-pe3999MWROP");
        cases.add("SKBpc3996-pb3999-pa3999-pe3999MWROP");
        cases.add("SKBpc3996MWROP");
        cases.add("SKBpc993996MWROP");
        cases.add("S3877Kpb3005-pc3876Bpb3886-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("S3877Kpc3876Bpb3885-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("SKBMpb20190731-pc20001107WROP");
        cases.add("SKBMpc202001107WROP");
        cases.add("SKBMpb202001107WROP");
        cases.add("SKBMpb20190630-pc20201107WROP");
        cases.add("SKBMpb20190630-pc20201107WROP");
        cases.add("S3877Kpb3005Bpb3881Mpb20190731-pc20001107W52Rpc3473-pb3415O2551P2680");
        cases.add("S3877Kpb3999-pc3876Bpb3885-pc3876Mpb20190730-pc20001107W52Rpc3473-pb3415O2551P2680");
        return cases;
    }
}
